﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
	/// <summary>
	/// 快速入库
	/// </summary>
	public class QuicklyInStockService : BaseService<BaseEntity>, IQuicklyInStockService
	{
		private readonly IQuicklyInStockRepository repository;
		public QuicklyInStockService(IQuicklyInStockRepository _repository) : base(_repository)
		{
			repository = _repository;
		}
		//输入库位验证库位
		public SetObjectDetail GetLocateByName(string locateName)
		{
			List<SysLocate> sysLocates = repository.GetLocateByName(locateName);

			List<SysStock> sysStocksLocateCount = repository.GetStockQtyByLocate(locateName);//库位在库数

			SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
			if (sysLocates.Count == 0)
			{
				//提示  [A1-2-2]:该库位不存在，请检查!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:该库位不存在，请检查!";
				return rfInfo;
			}
			else if (sysLocates[0].Enabled != "Y")
			{
				//提示  [A1-2-2]:该库位不可用，请检查!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:该库位不可用，请检查!";
				return rfInfo;
			}
			else if (sysLocates[0].LocateType != (int)WmsEnumUtil.LocateType.BASICS_LOCATE && sysLocates[0].LocateType != (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE && sysLocates[0].LocateType != (int)WmsEnumUtil.LocateType.COMBINATION_LOCATE)
			{
				//提示  [A1-2-2]:该库位不可用，请检查!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:该库位为" + sysLocates[0].LocateTypeDesc + "，请检查!";
				return rfInfo;
			}
			else if ((decimal)sysStocksLocateCount[0].Qty >= sysLocates[0].LocateCapacity)
			{
				//提示  [A1-2-2]:该库位容量已满，请检查!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:该库位容量已满，请检查!";
				return rfInfo;
			}

			rfInfo.type = "PASS";
			rfInfo.message = "[" + locateName + "]:扫描成功,请扫描箱条码";
			return rfInfo;
		}
		//输入条码，验证条码完成入库
		public SetObjectDetail CheckStockByCartonAlocate(string cartonNo, string locateName, string isGroup, string loginId)
		{
			SetObjectDetail rfInfo = new SetObjectDetail();//返回消息

			List<SysStock> sysStocks = repository.GetStockByCartonNo(cartonNo);//判断箱条码是否存在
			if (sysStocks.Count == 0)
			{
				//提示  [M0090105]:该条码不可用，请检查!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + cartonNo + "]:该条码不可用，请检查!";
				return rfInfo;
			}
			else if (sysStocks[0].Enabled != "Y")
			{
				//提示  [M0090105]:箱条码已冻结，无法操作!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + cartonNo + "]:箱条码已冻结，无法操作!";
				return rfInfo;
			}
			else if (sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.WAITING_INSTOCK)
			{
				//提示  [M0090105]:箱条码已冻结，无法操作!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + cartonNo + "]:是" + sysStocks[0].StockStatus + "状态，请检查!";
				return rfInfo;
			}

			List<SysLocate> sysLocates = repository.GetLocateAndPartByLocate(locateName);
			if (sysLocates.Count == 0)
			{
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:库位不存在，请重新输入!";
				return rfInfo;
			}

			List<SysStock> sysStocksLocateCount = repository.GetStockQtyByLocate(locateName);
			if ((decimal)sysStocksLocateCount[0].Qty > sysLocates[0].LocateCapacity)
			{
				//提示  [A1-2-2]:该库位容量已满，请检查！
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:该库位容量已满，请检查！";
				return rfInfo;
			}
			List<SysStock> sysstockByGroupNoCount = new List<SysStock>();
			//判断组托容量
			if (isGroup == "1")
			{
				if (!string.IsNullOrEmpty(sysStocks[0].GroupNo) && sysStocks[0].GroupNo != "NULL")
				{

					sysstockByGroupNoCount = repository.GetStockPartByGroupNo(sysStocks[0].GroupNo);//分组 零件号个数


					List<SysStock> sysStocks1 = repository.GetStockQtyByLocate(sysStocks[0].GroupNo);

					List<SysStock> sysStocksQtySum = repository.GetStockQtyByLocate(locateName);
					if ((decimal)(sysStocksQtySum[0].Qty + sysStocks1[0].Qty) > sysLocates[0].LocateCapacity)
					{
						//提示  [A1-2-2]:该库位容量不足，请检查！
						rfInfo.type = "FAIL";
						rfInfo.message = "[" + locateName + "]:该库位容量不足，请检查！";
						return rfInfo;
					}
					//基础库位、机动库位 大于1个零件
					if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE || sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)
					{
						if (sysstockByGroupNoCount.Count > 1)
						{
							//提示  该分组号零件号无法入到***库位，请检查!
							rfInfo.type = "FAIL";
							rfInfo.message = "[" + sysStocks[0].GroupNo + "]该分组号零件号无法入到[" + locateName + "]库位，请检查";
							return rfInfo;
						}
					}

					List<SysStock> sysStocks2 = repository.GetStockByGroupNo(sysStocks[0].GroupNo);
					for (int k = 0; k < sysStocks2.Count; k++)
					{
						string locatePartNo1 = sysLocates[0].PartNo;
						string stockPartNo1 = sysStocks2[k].PartNo;

						if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE)
						{
							if (locatePartNo1 != stockPartNo1)
							{
								if (sysStocks2.Count - 1 == k)
								{
									//提示  此箱码所对应零件与库位零件不匹配!
									rfInfo.type = "FAIL";
									rfInfo.message = "此箱码所对应零件与库位零件不匹配!";
									return rfInfo;
								}
							}
						}
						else if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)//机动库位
						{
							List<SysStock> sysStocks3 = repository.GetStockByLocateName(sysLocates[0].LocateName);
							for (int i = 0; i < sysStocks3.Count; i++)
							{
								if (sysStocks3[i].PartNo != "" && stockPartNo1 != sysStocks3[i].PartNo)
								{
									if (sysStocks2.Count - 1 == k)
									{
										//提示  此箱码所对应零件与库位零件不匹配!
										rfInfo.type = "FAIL";
										rfInfo.message = "[" + stockPartNo1 + "]该零件号无法入到机动库位，请检查!";
										return rfInfo;
									}
								}
							}
						}
					}
				}
				else
				{
					//提示  [A1-2-2]:该条码未分组，请检查！
					rfInfo.type = "FAIL";
					rfInfo.message = "[" + cartonNo + "]:该条码未分组，请检查！";
					return rfInfo;
				}
			}

			string locatePartNo = sysLocates[0].PartNo;
			string stockPartNo = sysStocks[0].PartNo;
			if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE)
			{
				if (locatePartNo != stockPartNo)
				{
					//提示  此箱码所对应零件与库位零件不匹配!
					rfInfo.type = "FAIL";
					rfInfo.message = "此箱码所对应零件与库位零件不匹配!";
					return rfInfo;
				}
			}
			else if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)//机动库位
			{
				List<SysStock> sysStocks1 = repository.GetStockByLocateName(sysLocates[0].LocateName);
				for (int i = 0; i < sysStocks1.Count; i++)
				{
					if (sysStocks1[i].PartNo != "" && stockPartNo != sysStocks1[i].PartNo)
					{
						//提示  此箱码所对应零件与库位零件不匹配!
						rfInfo.type = "FAIL";
						rfInfo.message = "[" + stockPartNo + "]该零件号无法入到机动库位，请检查!";
						return rfInfo;
					}
				}
			}
			List<string> sqlStrings = new List<string>();
			List<DynamicParameters> parameterList = new List<DynamicParameters>();

			StringBuilder stringBuilder = new StringBuilder();
			List<SysStock> sysstockByGroupNo = new List<SysStock>();
			if (isGroup == "1")
			{
				sysstockByGroupNo = repository.GetStockByGroupNo(sysStocks[0].GroupNo);

				for (int i = 0; i < sysstockByGroupNo.Count; i++)
				{
					stringBuilder = new StringBuilder();
					stringBuilder.Append("UPDATE sys_stock SET status=50,locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "'" +
						  ",zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "'" +
						",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysstockByGroupNo[i].CartonNo + "'");
					sqlStrings.Add(stringBuilder.ToString());

					stringBuilder = new StringBuilder();
					stringBuilder.Append("INSERT INTO dbo.sys_stock_trans ");
					stringBuilder.Append("( trans_code, ");
					stringBuilder.Append("    carton_no, ");
					stringBuilder.Append("    part_id, ");
					stringBuilder.Append("    part_no, ");
					stringBuilder.Append("    part_spec, ");
					stringBuilder.Append("    src_locate_id, ");
					stringBuilder.Append("    src_locate_name, ");
					stringBuilder.Append("    dest_locate_id, ");
					stringBuilder.Append("    dest_locate_name, ");
					stringBuilder.Append("    old_qty, ");
					stringBuilder.Append("    new_qty, ");
					stringBuilder.Append("    trans_qty, ");
					stringBuilder.Append("    old_status, ");
					stringBuilder.Append("    new_status, ");
					stringBuilder.Append("    unit, ");
					stringBuilder.Append("    factory_id, ");
					stringBuilder.Append("    factory_code, ");
					stringBuilder.Append("    src_erp_warehouse, ");
					stringBuilder.Append("    dest_erp_warehouse, ");
					stringBuilder.Append("    src_warehouse_id, ");
					stringBuilder.Append("    src_warehouse_name, ");
					stringBuilder.Append("    dest_warehouse_id, ");
					stringBuilder.Append("    dest_warehouse_name, ");
					stringBuilder.Append("    src_zone_id, ");
					stringBuilder.Append("    src_zone_name, ");
					stringBuilder.Append("    dest_zone_id, ");
					stringBuilder.Append("    dest_zone_name, ");
					stringBuilder.Append("    enabled, ");
					stringBuilder.Append("    create_userid, ");
					stringBuilder.Append("    create_time, ");
					stringBuilder.Append("    update_userid, ");
					stringBuilder.Append("    update_time, ");
					stringBuilder.Append("    guid ");
					stringBuilder.Append(") ");
					stringBuilder.Append("VALUES ");
					stringBuilder.Append("(   '60', ");
					stringBuilder.Append("    '" + sysstockByGroupNo[i].CartonNo + "', ");
					stringBuilder.Append("    " + sysstockByGroupNo[i].PartId + ", ");
					stringBuilder.Append("    '" + sysstockByGroupNo[i].PartNo + "',  ");
					stringBuilder.Append("    N'" + sysstockByGroupNo[i].PartSpec + "', ");
					stringBuilder.Append("    " + sysstockByGroupNo[i].LocateId == null ? 0 : sysstockByGroupNo[i].LocateId + ",  ");
					stringBuilder.Append("    '" + sysstockByGroupNo[i].LocateName + "',  ");
					stringBuilder.Append("    " + sysLocates[0].LocateId + ", ");
					stringBuilder.Append("    '" + sysLocates[0].LocateName + "',  ");
					stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
					stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
					stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
					stringBuilder.Append("    '40', ");
					stringBuilder.Append("    '50', ");
					stringBuilder.Append("    '" + sysstockByGroupNo[i].Unit + "', ");
					stringBuilder.Append("    " + sysstockByGroupNo[i].FactoryId + ", ");
					stringBuilder.Append("    '" + sysstockByGroupNo[i].FactoryCode + "',");
					stringBuilder.Append("    '" + sysstockByGroupNo[i].ErpWarehouse + "',");
					stringBuilder.Append("    '" + sysstockByGroupNo[i].ErpWarehouse + "', ");
					stringBuilder.Append("    " + sysstockByGroupNo[i].WarehouseId == null ? 0 : sysstockByGroupNo[i].WarehouseId + ", ");
					stringBuilder.Append("    '" + sysstockByGroupNo[i].WarehouseName + "', ");
					stringBuilder.Append("    " + sysLocates[0].WarehouseId + ", ");
					stringBuilder.Append("    '" + sysLocates[0].WarehouseName + "', ");
					stringBuilder.Append("    " + sysstockByGroupNo[i].ZoneId == null ? 0 : sysstockByGroupNo[i].ZoneId + ",  ");
					stringBuilder.Append("    '" + sysstockByGroupNo[i].ZoneName + "',  ");
					stringBuilder.Append("    " + sysLocates[0].ZoneId + ", ");
					stringBuilder.Append("    '" + sysLocates[0].ZoneName + "',  ");
					stringBuilder.Append("    'Y', ");
					stringBuilder.Append("    " + loginId + ", ");
					stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
					stringBuilder.Append("    " + loginId + ", ");
					stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
					stringBuilder.Append("    NEWID()  ");
					stringBuilder.Append("    ) ; ");
					sqlStrings.Add(stringBuilder.ToString());
				}
			}
			else
			{
				stringBuilder = new StringBuilder();
				stringBuilder.Append("UPDATE sys_stock SET status=50,locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "'" +
					  ",zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "'" +
					",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "';");
				sqlStrings.Add(stringBuilder.ToString());

				stringBuilder = new StringBuilder();
				stringBuilder.Append("INSERT INTO dbo.sys_stock_trans ");
				stringBuilder.Append("( ");
				stringBuilder.Append("    trans_code, ");
				stringBuilder.Append("    carton_no, ");
				stringBuilder.Append("    part_id, ");
				stringBuilder.Append("    part_no, ");
				stringBuilder.Append("    part_spec, ");
				stringBuilder.Append("    src_locate_id, ");
				stringBuilder.Append("    src_locate_name, ");
				stringBuilder.Append("    dest_locate_id, ");
				stringBuilder.Append("    dest_locate_name, ");
				stringBuilder.Append("    old_qty, ");
				stringBuilder.Append("    new_qty, ");
				stringBuilder.Append("    trans_qty, ");
				stringBuilder.Append("    old_status, ");
				stringBuilder.Append("    new_status, ");
				stringBuilder.Append("    unit, ");
				stringBuilder.Append("    factory_id, ");
				stringBuilder.Append("    factory_code, ");
				stringBuilder.Append("    src_erp_warehouse, ");
				stringBuilder.Append("    dest_erp_warehouse, ");
				stringBuilder.Append("    src_warehouse_id, ");
				stringBuilder.Append("    src_warehouse_name, ");
				stringBuilder.Append("    dest_warehouse_id, ");
				stringBuilder.Append("    dest_warehouse_name, ");
				stringBuilder.Append("    src_zone_id, ");
				stringBuilder.Append("    src_zone_name, ");
				stringBuilder.Append("    dest_zone_id, ");
				stringBuilder.Append("    dest_zone_name, ");
				stringBuilder.Append("    enabled, ");
				stringBuilder.Append("    create_userid, ");
				stringBuilder.Append("    create_time, ");
				stringBuilder.Append("    update_userid, ");
				stringBuilder.Append("    update_time, ");
				stringBuilder.Append("    guid ");
				stringBuilder.Append(") ");
				stringBuilder.Append("VALUES ");
				stringBuilder.Append("(   '60', ");
				stringBuilder.Append("    '" + sysStocks[0].CartonNo + "', ");
				stringBuilder.Append("    " + sysStocks[0].PartId + ", ");
				stringBuilder.Append("    '" + sysStocks[0].PartNo + "',  ");
				stringBuilder.Append("    N'" + sysStocks[0].PartSpec + "', ");
				stringBuilder.Append("    " + sysStocks[0].LocateId == null ? 0 : sysStocks[0].LocateId + ",  ");
				stringBuilder.Append("    '" + sysStocks[0].LocateName + "',  ");
				stringBuilder.Append("    " + sysLocates[0].LocateId + ", ");
				stringBuilder.Append("    '" + sysLocates[0].LocateName + "',  ");
				stringBuilder.Append("    " + sysStocks[0].Qty + ",");
				stringBuilder.Append("    " + sysStocks[0].Qty + ",");
				stringBuilder.Append("    " + sysStocks[0].Qty + ",");
				stringBuilder.Append("    '40', ");
				stringBuilder.Append("    '50', ");
				stringBuilder.Append("    '" + sysStocks[0].Unit + "', ");
				stringBuilder.Append("    " + sysStocks[0].FactoryId + ", ");
				stringBuilder.Append("    '" + sysStocks[0].FactoryCode + "',");
				stringBuilder.Append("    '" + sysStocks[0].ErpWarehouse + "',");
				stringBuilder.Append("    '" + sysStocks[0].ErpWarehouse + "', ");
				stringBuilder.Append("    " + sysStocks[0].WarehouseId == null ? 0 : sysStocks[0].WarehouseId + ", ");
				stringBuilder.Append("    '" + sysStocks[0].WarehouseName + "', ");
				stringBuilder.Append("    " + sysLocates[0].WarehouseId + ", ");
				stringBuilder.Append("    '" + sysLocates[0].WarehouseName + "', ");
				stringBuilder.Append("    " + sysStocks[0].ZoneId == null ? 0 : sysStocks[0].ZoneId + ",  ");
				stringBuilder.Append("    '" + sysStocks[0].ZoneName + "',  ");
				stringBuilder.Append("    " + sysLocates[0].ZoneId + ", ");
				stringBuilder.Append("    '" + sysLocates[0].ZoneName + "',  ");
				stringBuilder.Append("    'Y', ");
				stringBuilder.Append("    " + loginId + ", ");
				stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
				stringBuilder.Append("    " + loginId + ", ");
				stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
				stringBuilder.Append("    NEWID()  ");
				stringBuilder.Append("    ) ; ");
				sqlStrings.Add(stringBuilder.ToString());
			}




			int refNum = repository.SQLTransaction(sqlStrings, parameterList);
			if (refNum > 0)
			{
				if (isGroup == "1")
				{
					sysstockByGroupNo = repository.GetStockInByGroupNo(sysStocks[0].GroupNo);
				}
				else
				{
					sysstockByGroupNo = repository.GetStockInByCartonNo(sysStocks[0].CartonNo);
				}
				//提示  操作成功!请继续扫描箱条码
				rfInfo.type = "PASS";
				rfInfo.message = "操作成功!请继续扫描箱条码";
				rfInfo.wmsObjectDetail = sysstockByGroupNo.ConvertAll(s => (object)s);
				return rfInfo;
			}
			else
			{
				//提示  操作失败!
				rfInfo.type = "FAIL";
				rfInfo.message = "操作失败!请检查库存条码";
				return rfInfo;
			}
		}



		public SetObjectDetail CheckStockByCartonAlocateN(string cartonNo, string locateName, string isGroup, string loginId)
		{
			SetObjectDetail rfInfo = new SetObjectDetail();//返回消息

			List<SysStock> sysStocks = repository.GetStockByCartonNoN(cartonNo);//判断箱条码是否存在
			if (sysStocks.Count == 0)
			{
				//提示  [M0090105]:该条码不可用，请检查!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + cartonNo + "]:该条码不可用，请检查!";
				return rfInfo;
			}
			else if (sysStocks[0].Enabled != "Y")
			{
				//提示  [M0090105]:箱条码已冻结，无法操作!
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + cartonNo + "]:改条码已冻结，无法操作!";
				return rfInfo;
			}
			else if ( sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.WAITING_INSTOCK)
			{
				//提示  [M0090105]:条码状态不为待上架状态
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + cartonNo + "]:是" + sysStocks[0].StockStatus + "状态，请检查!";
				return rfInfo;
			}
			else if (sysStocks[0].CartonType == 0)
			{
				List<WmsRackPackage_Extend> WmsRackPackageInfos = repository.GetWmsRackPackageType(sysStocks[0].CartonNo).ToList();
				if (WmsRackPackageInfos.Count > 0)
				{
					//提示  条码有上级条码，请扫描上级条码!
					rfInfo.type = "FAIL";
					rfInfo.message = "条码有上级条码，请扫描上级条码：[" + WmsRackPackageInfos[0].RackNo + "]!";
					return rfInfo;
				}
				

			}
            else if (sysStocks[0].CartonType == 1)
            {
                List<WmsRackPackage_Extend> WmsRackPackageInfos = repository.GetWmsRackPackageType(sysStocks[0].CartonNo).ToList();
				if (WmsRackPackageInfos.Count > 0)
				{
					//提示  条码有上级条码，请扫描上级条码!
					rfInfo.type = "FAIL";
					rfInfo.message = "条码有上级条码，请扫描上级条码：[" + WmsRackPackageInfos[0].RackNo + "]!";
					return rfInfo;
				}
				
            }
			/*
            else if (sysStocks[0].CartonType != 0)
			{

				List<WmsRack_Extend> WmsRackPackageList = repository.GetRackTypeN(sysStocks[0].CartonNo).ToList();
				if (sysStocks[0].CartonType == 1) //周转箱
				{
					if (WmsRackPackageList.Count == 0)
					{
						//提示  [M0090105]:周转箱该条码不可用，请检查!
						rfInfo.type = "FAIL";
						rfInfo.message = "[" + cartonNo + "]:周转箱该条码不可用，请检查!";
						return rfInfo;
					}
				}
				if (sysStocks[0].CartonType == 2)//托
				{
					if (WmsRackPackageList.Count == 0)
					{
						//提示  [M0090105]:托该条码不可用，请检查!
						rfInfo.type = "FAIL";
						rfInfo.message = "[" + cartonNo + "]:托该条码不可用，请检查!";
						return rfInfo;
					}
				}


			}*/



			List<SysLocate> sysLocates = repository.GetLocateAndPartByLocate(locateName);
			if (sysLocates.Count == 0)
			{
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:库位不存在，请重新输入!";
				return rfInfo;
			}


			List<SysStock> sysStocksLocateCount = repository.GetStockQtyByLocate(locateName);//判断库位数量
			if ((decimal)sysStocksLocateCount[0].Qty > sysLocates[0].LocateCapacity)
			{
				//提示  [A1-2-2]:该库位容量已满，请检查！
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]:该库位容量已满，请检查！";
				return rfInfo;
			}
			List<SysStock> sysstockByGroupNoCount = new List<SysStock>();//分组
            List<SysStock> stocksInfos = new List<SysStock>();//不分组
            //判断组托容量
            if (isGroup == "1")// && sysStocks[0].CartonType!=2条码类型不能为组托
			{
				if (!string.IsNullOrEmpty(sysStocks[0].GroupNo) && sysStocks[0].GroupNo != "NULL")
				{

					sysstockByGroupNoCount = repository.GetStockPartByGroupNo(sysStocks[0].GroupNo);//分组 零件号个数


					List<SysStock> _sysStocks1 = repository.GetStockQtyByLocate(sysStocks[0].LocateName);//扫描条码库位的数量

					//List<SysStock> sysStocksQtySum = repository.GetStockQtyByLocate(locateName);//当前库位数量
					if ((decimal)(sysStocksLocateCount[0].Qty + _sysStocks1[0].Qty) > sysLocates[0].LocateCapacity)
					{
						//提示  [A1-2-2]:该库位容量不足，请检查！
						rfInfo.type = "FAIL";
						rfInfo.message = "[" + locateName + "]:该库位容量不足，请检查！";
						return rfInfo;
					}
					//基础库位、机动库位 大于1个零件
					if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE || sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)
					{
						if (sysstockByGroupNoCount.Count > 1)
						{
							//提示  该分组号零件号无法入到***库位，请检查!
							rfInfo.type = "FAIL";
							rfInfo.message = "[" + sysStocks[0].GroupNo + "]该分组号零件号无法入到[" + locateName + "]库位，请检查";
							return rfInfo;
						}
					}

					List<SysStock> sysStocks2 = repository.GetStockByGroupNo(sysStocks[0].GroupNo);
					for (int k = 0; k < sysStocks2.Count; k++)
					{
						string locatePartNo1 = sysLocates[0].PartNo;
						string stockPartNo1 = sysStocks2[k].PartNo;

						if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE)
						{
							if (locatePartNo1 != stockPartNo1)
							{
								if (sysStocks2.Count - 1 == k)
								{
									//提示  此箱码所对应零件与库位零件不匹配!
									rfInfo.type = "FAIL";
									rfInfo.message = "此箱码所对应零件与库位零件不匹配!";
									return rfInfo;
								}
							}
						}
						else if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)//机动库位
						{
							List<SysStock> sysStocks3 = repository.GetStockByLocateName(sysLocates[0].LocateName);
							for (int i = 0; i < sysStocks3.Count; i++)
							{
								if (sysStocks3[i].PartNo != "" && stockPartNo1 != sysStocks3[i].PartNo)
								{
									if (sysStocks2.Count - 1 == k)
									{
										//提示  此箱码所对应零件与库位零件不匹配!
										rfInfo.type = "FAIL";
										rfInfo.message = "[" + stockPartNo1 + "]该零件号无法入到机动库位，请检查!";
										return rfInfo;
									}
								}
							}
						}
					}
				}
				else
				{
					//提示  [A1-2-2]:该条码未分组，请检查！
					rfInfo.type = "FAIL";
					rfInfo.message = "[" + cartonNo + "]:该条码未分组，请检查！";
					return rfInfo;
				}
			}

			else
			{
                #region 循环判断条码是否符合规范
              
                repository.GetStockByRackListDown(cartonNo, null, ref stocksInfos);//根据条码找出它下级条码数据
                string locatePartNo = sysLocates[0].PartNo;
                List<SysStock> sysStocks1 = repository.GetStockByLocateName(sysLocates[0].LocateName);
                if (stocksInfos.Count() == 1 && stocksInfos[0].CartonType != 0)//是托或者是周转箱但没有包含箱条码
                {
                    //提示  周转箱条码没有包含的箱条码
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]该周转箱条码没有包含的箱条码，请检查!";
                    return rfInfo;
                }
                foreach (var item in stocksInfos)
                {
                    if (item.CartonType != 0)
                    {
                        continue;
                    }
                    string stockPartNo = item.PartNo;
                    if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE)
                    {
                        if (locatePartNo != stockPartNo)
                        {
                            //提示  此箱码所对应零件与库位零件不匹配!
                            rfInfo.type = "FAIL";
                            rfInfo.message = "此箱码所对应零件与库位零件不匹配!";
                            return rfInfo;
                        }
                    }
                    else if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)//机动库位
                    {

                        for (int i = 0; i < sysStocks1.Count; i++)
                        {
                            if (sysStocks1[i].PartNo != "" && stockPartNo != sysStocks1[i].PartNo)
                            {
                                //提示  此箱码所对应零件与库位零件不匹配!
                                rfInfo.type = "FAIL";
                                rfInfo.message = "[" + stockPartNo + "]该零件号无法入到机动库位，请检查!";
                                return rfInfo;
                            }
                        }
                    }

                }

                decimal sumQty = stocksInfos.Where(s => s.CartonType == 0).Sum(s => s.Qty);//箱条码的数量
                if ((sysLocates[0].LocateCapacity - (decimal)sysStocksLocateCount[0].Qty) < sumQty)//库位容量-当前库位数量<当前库位容量
                {
                    //提示 库位容量小于条码数量，请检查！
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + locateName + "]库位容量小于条码数量，请检查！";
                    return rfInfo;
                }

                #endregion
            }




            List<string> sqlStrings = new List<string>();
			List<DynamicParameters> parameterList = new List<DynamicParameters>();

			StringBuilder stringBuilder = new StringBuilder();
			List<SysStock> sysstockByGroupNo = new List<SysStock>();

			List<SysStock> sysStockInfos = new List<SysStock>();//所有箱条码的集合，用于接口数据

			if (isGroup == "1")// && sysStocks[0].CartonType != 2分组
            {
				sysstockByGroupNo = repository.GetUpdateStockByGroupNo(sysStocks[0].GroupNo);
				if (sysstockByGroupNo.Count > 0)
				{
					for (int i = 0; i < sysstockByGroupNo.Count; i++)
					{

						stringBuilder = new StringBuilder();
						stringBuilder.Append("UPDATE sys_stock SET status=50,locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "'" +
							  ",zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "'" +
							",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysstockByGroupNo[i].CartonNo + "';");
						sqlStrings.Add(stringBuilder.ToString());

						stringBuilder = new StringBuilder();
						stringBuilder.Append("INSERT INTO dbo.sys_stock_trans ");
						stringBuilder.Append("( ");
						stringBuilder.Append("    trans_code, ");
						stringBuilder.Append("    carton_no, ");
						stringBuilder.Append("    part_id, ");
						stringBuilder.Append("    part_no, ");
						stringBuilder.Append("    part_spec, ");
						stringBuilder.Append("    src_locate_id, ");
						stringBuilder.Append("    src_locate_name, ");
						stringBuilder.Append("    dest_locate_id, ");
						stringBuilder.Append("    dest_locate_name, ");
						stringBuilder.Append("    old_qty, ");
						stringBuilder.Append("    new_qty, ");
						stringBuilder.Append("    trans_qty, ");
						stringBuilder.Append("    old_status, ");
						stringBuilder.Append("    new_status, ");
						stringBuilder.Append("    unit, ");
						stringBuilder.Append("    factory_id, ");
						stringBuilder.Append("    factory_code, ");
						stringBuilder.Append("    src_erp_warehouse, ");
						stringBuilder.Append("    dest_erp_warehouse, ");
						stringBuilder.Append("    src_warehouse_id, ");
						stringBuilder.Append("    src_warehouse_name, ");
						stringBuilder.Append("    dest_warehouse_id, ");
						stringBuilder.Append("    dest_warehouse_name, ");
						stringBuilder.Append("    src_zone_id, ");
						stringBuilder.Append("    src_zone_name, ");
						stringBuilder.Append("    dest_zone_id, ");
						stringBuilder.Append("    dest_zone_name, ");
						stringBuilder.Append("    enabled, ");
						stringBuilder.Append("    create_userid, ");
						stringBuilder.Append("    create_time, ");
						stringBuilder.Append("    update_userid, ");
						stringBuilder.Append("    update_time, ");
						stringBuilder.Append("    guid ");
						stringBuilder.Append(") ");
						stringBuilder.Append("VALUES ");
						stringBuilder.Append("(   '60', ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].CartonNo + "', ");
						stringBuilder.Append("    " + sysstockByGroupNo[i].PartId + ", ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].PartNo + "',  ");
						stringBuilder.Append("    N'" + sysstockByGroupNo[i].PartSpec + "', ");
						stringBuilder.Append("    '" + (sysstockByGroupNo[i].LocateId == null ? 0 : sysstockByGroupNo[i].LocateId) + "',  ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].LocateName + "',  ");
						stringBuilder.Append("    " + sysLocates[0].LocateId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].LocateName + "',  ");
						stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
						stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
						stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
						stringBuilder.Append("    '40', ");
						stringBuilder.Append("    '50', ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].Unit + "', ");
						stringBuilder.Append("    " + sysstockByGroupNo[i].FactoryId + ", ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].FactoryCode + "',");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].ErpWarehouse + "',");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].ErpWarehouse + "', ");
						stringBuilder.Append("    '" + (sysstockByGroupNo[i].WarehouseId == null ? 0 : sysstockByGroupNo[i].WarehouseId) + "', ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].WarehouseName + "', ");
						stringBuilder.Append("    " + sysstockByGroupNo[0].WarehouseId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].WarehouseName + "', ");
						stringBuilder.Append("    " + sysstockByGroupNo[i].ZoneId == null ? 0 : sysstockByGroupNo[i].ZoneId + ",  ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].ZoneName + "',  ");
						stringBuilder.Append("    " + sysLocates[0].ZoneId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].ZoneName + "',  ");
						stringBuilder.Append("    'Y', ");
						stringBuilder.Append("    " + loginId + ", ");
						stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
						stringBuilder.Append("    " + loginId + ", ");
						stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
						stringBuilder.Append("    NEWID()  ");
						stringBuilder.Append("    ) ; ");
						sqlStrings.Add(stringBuilder.ToString());

						if (sysstockByGroupNo[i].CartonType == 0)
						{
							sysStockInfos.Add(sysstockByGroupNo[i]);//所有箱条码的集合，用于接口数据
						}
					}

				}

			}
			else
			{
				if (stocksInfos.Count > 0)
				{
					//List<SysStock> sysStockInfo=new List<SysStock>();
					//sysStockInfo=repository.GetWmsByStock(sysStocks[0].CartonNo);//找到周转箱
					//if ( sysStockInfo.Count()>0)
					//{
					//	sysStocks.Add(sysStockInfo[0]);
					//}

					sysStocks = stocksInfos;//将所有的条码信息给sotck条码信息

                    for (int i = 0; i < sysStocks.Count; i++)
					{
						if (sysStocks[i].CartonType == 0)
						{
							sysStockInfos.Add(sysStocks[i]);//所有箱条码的集合，用于接口数据
						}

						stringBuilder = new StringBuilder();
						stringBuilder.Append("UPDATE sys_stock SET status=50,locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "'" +
							  ",zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "'" +
							",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysStocks[i].CartonNo + "';");
						sqlStrings.Add(stringBuilder.ToString());

						stringBuilder = new StringBuilder();
						stringBuilder.Append("INSERT INTO dbo.sys_stock_trans ");
						stringBuilder.Append("( ");
						stringBuilder.Append("    trans_code, ");
						stringBuilder.Append("    carton_no, ");
						stringBuilder.Append("    part_id, ");
						stringBuilder.Append("    part_no, ");
						stringBuilder.Append("    part_spec, ");
						stringBuilder.Append("    src_locate_id, ");
						stringBuilder.Append("    src_locate_name, ");
						stringBuilder.Append("    dest_locate_id, ");
						stringBuilder.Append("    dest_locate_name, ");
						stringBuilder.Append("    old_qty, ");
						stringBuilder.Append("    new_qty, ");
						stringBuilder.Append("    trans_qty, ");
						stringBuilder.Append("    old_status, ");
						stringBuilder.Append("    new_status, ");
						stringBuilder.Append("    unit, ");
						stringBuilder.Append("    factory_id, ");
						stringBuilder.Append("    factory_code, ");
						stringBuilder.Append("    src_erp_warehouse, ");
						stringBuilder.Append("    dest_erp_warehouse, ");
						stringBuilder.Append("    src_warehouse_id, ");
						stringBuilder.Append("    src_warehouse_name, ");
						stringBuilder.Append("    dest_warehouse_id, ");
						stringBuilder.Append("    dest_warehouse_name, ");
						stringBuilder.Append("    src_zone_id, ");
						stringBuilder.Append("    src_zone_name, ");
						stringBuilder.Append("    dest_zone_id, ");
						stringBuilder.Append("    dest_zone_name, ");
						stringBuilder.Append("    enabled, ");
						stringBuilder.Append("    create_userid, ");
						stringBuilder.Append("    create_time, ");
						stringBuilder.Append("    update_userid, ");
						stringBuilder.Append("    update_time, ");
						stringBuilder.Append("    guid ");
						stringBuilder.Append(") ");
						stringBuilder.Append("VALUES ");
						stringBuilder.Append("(   '60', ");
						stringBuilder.Append("    '" + sysStocks[i].CartonNo + "', ");
						stringBuilder.Append("    " + sysStocks[i].PartId + ", ");
						stringBuilder.Append("    '" + sysStocks[i].PartNo + "',  ");
						stringBuilder.Append("    N'" + sysStocks[i].PartSpec + "', ");
						stringBuilder.Append("    '" + (sysStocks[i].LocateId == null ? 0 : sysStocks[i].LocateId) + "',  ");
						stringBuilder.Append("    '" + sysStocks[i].LocateName + "',  ");
						stringBuilder.Append("    " + sysLocates[0].LocateId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].LocateName + "',  ");
						stringBuilder.Append("    " + sysStocks[i].Qty + ",");
						stringBuilder.Append("    " + sysStocks[i].Qty + ",");
						stringBuilder.Append("    " + sysStocks[i].Qty + ",");
						stringBuilder.Append("    '40', ");
						stringBuilder.Append("    '50', ");
						stringBuilder.Append("    '" + sysStocks[i].Unit + "', ");
						stringBuilder.Append("    " + sysStocks[i].FactoryId + ", ");
						stringBuilder.Append("    '" + sysStocks[i].FactoryCode + "',");
						stringBuilder.Append("    '" + sysStocks[i].ErpWarehouse + "',");
						stringBuilder.Append("    '" + sysStocks[i].ErpWarehouse + "', ");
						stringBuilder.Append("    '" + (sysStocks[i].WarehouseId == null ? 0 : sysStocks[i].WarehouseId) + "', ");
						stringBuilder.Append("    '" + sysStocks[i].WarehouseName + "', ");
						stringBuilder.Append("    " + sysLocates[0].WarehouseId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].WarehouseName + "', ");
						stringBuilder.Append("    " + sysStocks[i].ZoneId == null ? 0 : sysStocks[i].ZoneId + ",  ");
						stringBuilder.Append("    '" + sysStocks[i].ZoneName + "',  ");
						stringBuilder.Append("    " + sysLocates[0].ZoneId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].ZoneName + "',  ");
						stringBuilder.Append("    'Y', ");
						stringBuilder.Append("    " + loginId + ", ");
						stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
						stringBuilder.Append("    " + loginId + ", ");
						stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
						stringBuilder.Append("    NEWID()  ");
						stringBuilder.Append("    ) ; ");
						sqlStrings.Add(stringBuilder.ToString());



					}


				}


			}


			var _sysStockInfos = sysStockInfos.GroupBy(x => x.PartId).Select(s => (new { PartId = s.Key, qtySum = s.Sum(item => item.Qty), RecQtySum = s.Sum(item => Convert.ToDecimal(item.RecQty)) })).ToList();


			for (int i = 0; i < _sysStockInfos.Count(); i++)
			{

				SysStock sys = sysStockInfos.Where(s => s.PartId == _sysStockInfos[i].PartId).First();
				List<WmsOutstockDetail> outstockDetail = repository.GetOutstockDetailList(sys.RecCartonQty, _sysStockInfos[i].PartId.ToString()).ToList();

				stringBuilder = new StringBuilder();
				stringBuilder.Append("INSERT INTO dbo.WMS_DSPOS ");
				stringBuilder.Append("( EBELN, ");//通知单号
				stringBuilder.Append("    POVBELN, ");//采购订单号
				stringBuilder.Append("    EVRTP, ");//行号
				stringBuilder.Append("    LIFNR, ");//供应商代码
				stringBuilder.Append("    MATNR, ");//物料编码
				stringBuilder.Append("    ZDEV_NUM, ");//订单数量
				stringBuilder.Append("    UNIT, ");//计量单位
				stringBuilder.Append("    PSTYP, ");//特殊采购标识  1：委外，2：采购
				stringBuilder.Append("    ZDATE, ");//计划交货日期
				stringBuilder.Append("    ZCJSJ, ");//计划交货时间
				stringBuilder.Append("    WERKS, ");//工厂代码
				stringBuilder.Append("    LGORT, ");//收货库
				stringBuilder.Append("    ZSTAS, ");//状态  1、已收货，2、未收货
				stringBuilder.Append("    MLOGO, ");//收货数量
													//stringBuilder.Append("    DATAST, ");
													//stringBuilder.Append("    SHNUM, ");
				stringBuilder.Append("    SID, ");
				stringBuilder.Append("    ACTTIM, ");
				stringBuilder.Append("    ACTUSR, ");
				stringBuilder.Append("    STAFLG, ");
				stringBuilder.Append("    guid ");
				stringBuilder.Append(") ");
				stringBuilder.Append("VALUES ");
				stringBuilder.Append(" (   '" + sys.CartonNo + "', ");
				stringBuilder.Append("    '" + sys.RefOrderNo + "', ");
				stringBuilder.Append("    '" + (outstockDetail.Count == 0 ? "" : outstockDetail[0].ItemNo) + "',  ");
				stringBuilder.Append("    '" + sys.VendorCode + "', ");
				stringBuilder.Append("    '" + sys.PartNo + "',");
				stringBuilder.Append("    '" + _sysStockInfos[i].qtySum + "',  ");
				stringBuilder.Append("    '" + sys.Unit + "', ");
				stringBuilder.Append("    '" + 2 + "',  ");
				stringBuilder.Append("    '" + (outstockDetail.Count == 0 ? "" : outstockDetail[0].PlanDate) + "',");
				stringBuilder.Append("    '" + (outstockDetail.Count == 0 ? "" : outstockDetail[0].PlanTime) + "',");
				stringBuilder.Append("    '" + sys.FactoryCode + "',");
				stringBuilder.Append("    '" + sys.ZoneName + "', ");//收货库 (库区)
				stringBuilder.Append("    '1', ");//1、已收货，2、未收货
				stringBuilder.Append("    '" + _sysStockInfos[i].RecQtySum + "', ");
				stringBuilder.Append("'" + Guid.NewGuid().ToString().Replace("-", "") + "',");
				stringBuilder.Append("'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',");
				stringBuilder.Append("'" + loginId + "',");
				stringBuilder.Append("'" + 0 + "',");
				stringBuilder.Append("    NEWID()  ");
				stringBuilder.Append("    ) ; ");
				sqlStrings.Add(stringBuilder.ToString());
			}


			int refNum = repository.SQLTransaction(sqlStrings, parameterList);
			if (refNum > 0)
			{
				if (isGroup == "1")
				{
					sysstockByGroupNo = repository.GetStockInByGroupNo(sysStocks[0].GroupNo);
				}
				else
				{
					sysstockByGroupNo = repository.GetStockInByCartonNo(sysStocks[0].CartonNo);
				}
				//提示  操作成功!请继续扫描箱条码
				rfInfo.type = "PASS";
				rfInfo.message = "操作成功!请继续扫描箱条码";
				rfInfo.wmsObjectDetail = sysstockByGroupNo.ConvertAll(s => (object)s);
				return rfInfo;
			}
			else
			{
				//提示  操作失败!
				rfInfo.type = "FAIL";
				rfInfo.message = "操作失败!请检查库存条码";
				return rfInfo;
			}

		}


	}
}
